{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "distinguished-drama",
   "metadata": {},
   "source": [
    "https://leetcode.com/problems/binary-gap\n",
    "\n",
    "\n",
    "Runtime: 8 ms, faster than 7.07% of C++ online submissions for Binary Gap.\n",
    "Memory Usage: 9.5 MB, less than 11.64% of C++ online submissions for Binary Gap.\n",
    "\n",
    "\n",
    "```cpp\n",
    "#include <vector>\n",
    "#include <algorithm>\n",
    "#include <iostream>\n",
    "#include <bitset>\n",
    "#include <regex>\n",
    "\n",
    "using namespace std;\n",
    "\n",
    "class Solution {\n",
    "public:\n",
    "    int binaryGap(int n) {\n",
    "        //8:39\n",
    "        std::string s = std::bitset<64>(n).to_string(); // string conversion\n",
    "        for(int i=0; i<s.size(); i++) {\n",
    "            if (s[i] == '1') {\n",
    "                s = s.substr(i, s.size()-i);\n",
    "                break;\n",
    "            }\n",
    "        }\n",
    "        //cout << s << endl;\n",
    "\n",
    "        std::string input_seq = s;\n",
    "        std::regex re(\"1(?=(0+)1)\"); // <-- working\n",
    "        //std::regex re(\"1(0+)1\"); // <-- not work\n",
    "        std::sregex_iterator next(input_seq.begin(), input_seq.end(), re);\n",
    "        std::sregex_iterator end;\n",
    "        int longest = 0;\n",
    "        while (next != end)\n",
    "        {\n",
    "            std::smatch match = *next;\n",
    "            string the_string = match.str(1);\n",
    "            if (the_string.size() > longest) {\n",
    "                longest = the_string.size();\n",
    "            }\n",
    "            /*\n",
    "            std::cout << match.str(1) << \"\\t\"\n",
    "                      << \"\\t\" << match.position() << \"\\t\"\n",
    "                      << \"\\n\"; // <-- SEE HERE\n",
    "            */\n",
    "            next++;\n",
    "        }\n",
    "        if (longest != 0) {\n",
    "            return longest + 1;\n",
    "        } else {\n",
    "            if (s.find(\"11\") != string::npos) {\n",
    "                return 1;\n",
    "            } else {\n",
    "                return 0;\n",
    "            }\n",
    "        }\n",
    "        return 0;\n",
    "        //8:56\n",
    "    }\n",
    "};\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "alternate-mouth",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "C++17",
   "language": "C++17",
   "name": "xcpp17"
  },
  "language_info": {
   "codemirror_mode": "text/x-c++src",
   "file_extension": ".cpp",
   "mimetype": "text/x-c++src",
   "name": "c++",
   "version": "17"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
